package com.lz.sports.db

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import com.lz.sports.bean.DeviceInfo

@Dao
interface DeviceInfoDao {

    //添加扫描设备
    @Insert
    fun addScanDeviceInfoList(infoList: List<DeviceInfo>)

    //查询该设备mac是否已经录入
    @Query("select COUNT(*) from DeviceInfo where mac = :mac and projectId = :projectId")
    fun hasDeviceInfo(mac: String, projectId: Int): Long

    //查找-分页
    @Query("select * from DeviceInfo where projectId = :projectId limit :page, :count")
    fun findDeviceInfoList(projectId: Int, page: Int, count: Int): List<DeviceInfo>

    //模糊查找所有-分页
    @Query("select * from DeviceInfo where projectId = :projectId and (name like '%' || :keyword || '%' or alias like '%' || :keyword || '%' or mac like '%' || :keyword || '%') limit :page, :count")
    fun findDeviceInfoList(keyword: String, projectId: Int, page: Int, count: Int): List<DeviceInfo>

    //找出所有辅助设备
    @Query("select * from DeviceInfo where groupId = ${DeviceInfo.ASSIST_ID} and projectId = :projectId")
    fun findAssistDeviceInfoList(projectId: Int): List<DeviceInfo>
















    //模糊查找所有-分页
    @Query("select * from DeviceInfo where name like '%' || :keyword || '%' or alias like '%' || :keyword || '%' or mac like '%' || :keyword || '%' limit :page, :count")
    fun findDeviceInfoList(keyword: String, page: Int, count: Int): List<DeviceInfo>

    //查找-分页
    @Query("select * from DeviceInfo limit :page, :count")
    fun findDeviceInfoList(page: Int, count: Int): List<DeviceInfo>

    //通过ID删除
    @Query("delete from DeviceInfo where id = :infoId")
    fun deleteDeviceInfo(infoId: Int): Int

    //更新设备库
    @Update
    fun updateDeviceInfo(deviceInfo: DeviceInfo): Int

    //删除设备分组,全置默认值
    @Query("update DeviceInfo set groupId = ${DeviceInfo.UNKNOWN_ID} where groupId = :groupId")
    fun deleteGroup(groupId: Int)

    //模糊查找所有未绑定分组的
    @Query("select * from DeviceInfo where groupId = ${DeviceInfo.UNKNOWN_ID} and projectId = :projectId and (name like '%' || :keyword || '%' or alias like '%' || :keyword || '%' or mac like '%' || :keyword || '%')")
    fun findUnBindDeviceInfoList(projectId: Int, keyword: String): List<DeviceInfo>

    //查找所有未绑定分组的
    @Query("select * from DeviceInfo where groupId = ${DeviceInfo.UNKNOWN_ID} and projectId = :projectId")
    fun findUnBindDeviceInfoList(projectId: Int): List<DeviceInfo>

    //绑定设备组
    @Query("update DeviceInfo set groupId = :groupId, number = :number where id = :id")
    fun bindDeviceGroup(groupId: Int, id: Int, number: Int)

    //取消绑定组
    @Query("update DeviceInfo set groupId = ${DeviceInfo.UNKNOWN_ID}, number = 0 where groupId = :groupId")
    fun unbindDeviceGroup(groupId: Int)

    //取消绑定设备
    @Query("update DeviceInfo set groupId = ${DeviceInfo.UNKNOWN_ID}, number = 0 where id = :id")
    fun unbindDeviceInfo(id: Int)

    //修改序号
    @Query("update DeviceInfo set number = :number where id = :id")
    fun modifyNumber(number: Int, id: Int)

    //找出所有分组的设备
    @Query("select * from DeviceInfo where groupId > 0 order by groupId")
    fun findDeviceInfoList(): List<DeviceInfo>

    //通过设备组查找设备
    @Query("select * from DeviceInfo where groupId = :groupId")
    fun findDeviceInfoListByGroupId(groupId: Int): List<DeviceInfo>
}